#include "CSprite.h"
#include "CFrame.h"
#include "CCore.h"
#include "CVideoManager.h"
#include "CLayer.h"
#include <stdarg.h>

CSprite::CSprite()
:bCanDelete(false)
{

    bVisible = true;
    sCurrentAnim = -1;
    pr_Layer = NULL;
    pr_Image = NULL;
}

CSprite::~CSprite()
{
    Release();
}

bool CSprite::Release()
{
    if (pr_Image)
        CVideoManager::getInstance()->DeleteImage(&pr_Image);
    ClearAnimations();
    return true;
}

bool CSprite::Create(const std::string&  cp_chFileName,int h, int w)
{
    return Create(cp_chFileName,CVector(h,w));
}

bool CSprite::Create(const std::string&  cp_chFileName,const CVector& cFrameSize)
{
    pr_Image = CVideoManager::getInstance()->LoadImage(cp_chFileName);
    pr_Image->SetFrameSize(cFrameSize);
    return true;
}

bool CSprite::Create(CImage* imagePam)
{
    pr_Image = imagePam;
    return true;
}
void CSprite::Draw()
{
    CVector Pos;
    if (pr_Layer)
        Pos = pr_Layer->GetBackBufferPos(Position,pr_Image->FrameSize);
    else
        Pos = Position;
    Pos.Floor();
    if (sCurrentAnim >= 0 && sCurrentAnim < (int)Animations.size())
        CVideoManager::getInstance()->Screen.DrawImage(Pos,pr_Image,Animations[sCurrentAnim]->GetCurrentFrame());
    else
        CVideoManager::getInstance()->Screen.DrawImage(Pos,pr_Image,0);
}

void CSprite::Update()
{
    if ((sCurrentAnim >= 0) && (sCurrentAnim < (int)Animations.size()))
        Animations[sCurrentAnim]->Update();
}

void CSprite::AddAnimation(int iFPS,bool bRepeat, int iFrameCount,...)
{
    CAnimation *p_Animation = new CAnimation();

    p_Animation->bRepeat = bRepeat;
    p_Animation->SetFrameInterval(iFPS,0);

    va_list marker;
    va_start(marker, iFrameCount);

    for (int i = 0; i < iFrameCount; i++)
        p_Animation->Frames.push_back(va_arg(marker, int));

    va_end( marker );
    Animations.push_back(p_Animation);
}

void CSprite::AddAnimationRange(int iFPS,bool bRepeat, int iStartRange, int iEndRange)
{
    CAnimation *p_Animation = new CAnimation();
    p_Animation->bRepeat = bRepeat;
    p_Animation->SetFrameInterval(iFPS,0);

    for (int i = iStartRange; i < iEndRange; i++)
        p_Animation->Frames.push_back(i);

    Animations.push_back(p_Animation);
}

void CSprite::ResetAnimation()
{
    if (sCurrentAnim >= 0 && sCurrentAnim < (int)Animations.size())
        Animations[sCurrentAnim]->Reset();
}

void CSprite::SetCurrentAnimation(short sAnim)
{
    if (sAnim != sCurrentAnim){
        sCurrentAnim = sAnim;
        ResetAnimation();
    }
}

void CSprite::ClearAnimations()
{
    for (int i = (int)Animations.size()-1; i >=0; i--)
        delete Animations[i];
    Animations.clear();
}

CFrame CSprite::GetFrame()
{
    CFrame Frame;
    Frame.iLeft   = (int)Position.fx;
    Frame.iTop    = (int)Position.fy;
    Frame.iRight  = (int)(Position.fx + pr_Image->FrameSize.fx);
    Frame.iBottom = (int)(Position.fy + pr_Image->FrameSize.fy);
    return Frame;
}

CVector& CSprite::GetSize()
{
    return pr_Image->FrameSize;
}

void CSprite::Execute()
{

}


